﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Drawing.Drawing2D;

namespace OpenComposite.Workflow
{
	[Designer(typeof(NewLineActivityDesigner))]
	[ToolboxItem(typeof(NewLineActivityToolboxItem))]
	[ToolboxBitmap(typeof(resfinder), "OpenComposite.Workflow.Resources.newLine.png")]
	public partial class NewLineActivity : Activity
	{
		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		{
			return base.Execute(executionContext);
		}
	}

	public class NewLineActivityDesigner : ActivityDesigner
	{
		private const int width = 20;
		private const int height = 20;
		private readonly Size _minimumSize = new Size(width, height);

		public override bool CanBeParentedTo(CompositeActivityDesigner parentActivityDesigner)
		{
			return true;
		}

		protected override void OnPaint(ActivityDesignerPaintEventArgs e)
		{
			base.OnPaint(e);

			// paint new line indicators
			GraphicsPath shape = ActivityDesignerPaint.GetRoundedRectanglePath(Bounds, 10);
			using ( LinearGradientBrush brush = new LinearGradientBrush(Bounds, e.DesignerTheme.BackColorStart, e.DesignerTheme.BackColorEnd, e.DesignerTheme.BackgroundStyle) ) {
				e.Graphics.FillPath(brush, shape);
				e.Graphics.DrawPath(e.DesignerTheme.ForegroundPen, shape);
			}

			int centerY = WorkflowHelpers.GetCenter(Bounds).Y;
			int distance = 5;
			int shortDistance = 4;
			// --
			e.Graphics.DrawLine(e.DesignerTheme.ForegroundPen,
				Bounds.Left + shortDistance, Bounds.Top + distance, Bounds.Right - distance, Bounds.Top + distance);
			//   |
			e.Graphics.DrawLine(e.DesignerTheme.ForegroundPen,
				Bounds.Right - distance, Bounds.Top + distance, Bounds.Right - distance, centerY);
			//  -
			e.Graphics.DrawLine(e.DesignerTheme.ForegroundPen,
				Bounds.Right - distance, centerY, Bounds.Left + distance, centerY);
			// |
			e.Graphics.DrawLine(e.DesignerTheme.ForegroundPen,
				Bounds.Left + distance, centerY, Bounds.Left + distance, Bounds.Bottom - distance);
			//  --
			e.Graphics.DrawLine(e.DesignerTheme.ForegroundPen,
				Bounds.Left + distance, Bounds.Bottom - distance, Bounds.Right - shortDistance, Bounds.Bottom - distance);
		}

		public override Size MinimumSize
		{
			get
			{
				return base.MinimumSize;
			}
		}

		protected override void OnLayoutPosition(ActivityDesignerLayoutEventArgs e)
		{
			base.OnLayoutPosition(e);
		}

		protected override Size OnLayoutSize(ActivityDesignerLayoutEventArgs e)
		{
			return base.OnLayoutSize(e); ;
		}
	}

	public class NewLineActivityDesignerTheme : ActivityDesignerTheme
	{
		public NewLineActivityDesignerTheme(WorkflowTheme theme)
			: base(theme)
		{
			this.BackColorStart = Color.White;
			this.BackColorEnd = Color.CornflowerBlue;
			this.BackgroundStyle = LinearGradientMode.ForwardDiagonal;
		}
	}

	public class NewLineActivityToolboxItem : ActivityToolboxItem
	{
		public NewLineActivityToolboxItem(Type type)
			: base(type)
		{
			this.DisplayName = "New Line";
		}
	}
}
